package cn.itcast.bassm.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

import cn.itcast.bassm.mapper.UserMapper;


public class Testsss {

	@Autowired
	private  UserMapper userMapper;

	public static void main(String[] args) {
		String imgpath="https://wwl.ycws.cc/image/upload/2017110717231503.o6zAJs2CE-J6LacyBOOVsryIP2Hc.ca93f135027c80f95a24ed787d8a769c.jpg";
		try {
			detectFace(imgpath);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static List<String> detectFace(String imgpath) throws Throwable {
	     String feature = null;
	        String face_image = null;
	        List<String> retList = new ArrayList<String>();
	        String json = null;
	        String wximgpath = getdownimg(imgpath);//下载相片
	        if(wximgpath!=null){//判断是不是下载图片出现问题了

	        }else {
	        	retList=null;
			}
	        return retList;
	}

	/**
	 * 通过url下载图片
	 */
	public static String getdownimg(String wxpath)throws Exception{
		Date data=new Date();
		String filename;
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        String newData = dateFormat.format(data);
		try {
			filename="C:/"+newData+"01"+".jpg";
			// 构造URL
			URL url = new URL(wxpath);
			// 打开连接
			URLConnection con = url.openConnection();
			// 输入流
			InputStream is = con.getInputStream();
			// 1K的数据缓冲
			byte[] bs = new byte[1024];
			// 读取到的数据长度
			int len;
			// 输出的文件流
			OutputStream os = new FileOutputStream(filename);
			// 开始读取
			while ((len = is.read(bs)) != -1) {
			  os.write(bs, 0, len);
			}
			// 完毕，关闭所有链接
			os.close();
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			filename=null;
		}
	    return filename;
	}


}
